package com.wangyp.tower.tower.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.regexp.internal.RE;
import com.wangyp.tower.common.result.Result;
import com.wangyp.tower.model.tower.TowerInfo;
import com.wangyp.tower.model.user.UserInfo;
import com.wangyp.tower.tower.service.TowerInfoService;
import com.wangyp.tower.vo.tower.TowerInfoQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Author: Wangyp
 * @Date: 2021/4/26 19:21
 * @Description: *
 */
@Api(tags = "铁塔管理")
@RestController
@RequestMapping("/admin/tower/towerInfo")
public class TowerInfoController {

    @Autowired
    private TowerInfoService towerInfoService;

    @ApiOperation(value = "获取所有铁塔信息")
    @GetMapping("findAll")
    public Result findAllTowerInfo() {
        List<TowerInfo> list = towerInfoService.list();
        return Result.ok(list);
    }

    @ApiOperation(value = "获取信号塔数量")
    @GetMapping("getTowerInfoAcount")
    public Result getTowerInfoAcount() {
        int count = towerInfoService.count();
        System.out.println(count);
        return Result.ok(count);
    }

    @ApiOperation(value = "逻辑删除铁塔信息")
    @DeleteMapping("{id}")
    public Result removeTowerInfo(@PathVariable Long id) {
        boolean flag = towerInfoService.removeById(id);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "分页查询铁塔信息")
    @PostMapping("findPageTowerInfo/{current}/{limit}/{name}")
    public Result findPageTowerInfo(@PathVariable long current,
                                    @PathVariable long limit,
                                    @PathVariable String name,
                                    @RequestBody(required = false) TowerInfoQueryVo towerInfoQueryVo){
        return Result.ok(towerInfoService.findPageTowerInfo(current, limit, name, towerInfoQueryVo));
    }

    @ApiOperation(value = "分页查询启用铁塔信息")
    @PostMapping("findPageEnableTowerInfo/{current}/{limit}")
    public Result findPageEnableTowerInfo(@PathVariable long current,
                                    @PathVariable long limit,
                                    @RequestBody(required = false) TowerInfoQueryVo towerInfoQueryVo){
        return Result.ok(towerInfoService.findPageEnableTowerInfo(current, limit, towerInfoQueryVo));
    }

    @ApiOperation(value = "分页查询未启用铁塔信息")
    @PostMapping("findPageNotEnableTowerInfo/{current}/{limit}")
    public Result findPageNotEnableTowerInfo(@PathVariable long current,
                                    @PathVariable long limit,
                                    @RequestBody(required = false) TowerInfoQueryVo towerInfoQueryVo){
        return Result.ok(towerInfoService.findPageNotEnableTowerInfo(current, limit, towerInfoQueryVo));
    }

    @ApiOperation(value = "添加铁塔信息")
    @PostMapping("saveTowerInfo")
    public Result saveTowerInfo(@RequestBody TowerInfo towerInfo) {
        if (towerInfoService.addTowerInfo(towerInfo)) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "根据id搜索铁塔")
    @GetMapping("getTowerInfoById/{id}")
    public Result getTowerInfoById(@PathVariable Long id) {
        TowerInfo towerInfo = towerInfoService.getById(id);
        System.out.println("getTower"  + id);
        if (StringUtils.isEmpty(towerInfo)) {
            return Result.fail();
        } else {
            return Result.ok(towerInfo);
        }
    }

    @ApiOperation(value = "模糊搜索铁塔")
    @PostMapping("getTowerInfoByCountyCode")
    public Result getTowerInfoByCountyCode( @RequestBody TowerInfoQueryVo towerInfoQueryVo) {
        TowerInfo towerInfo = towerInfoService.getByCountyCode(towerInfoQueryVo);
        if (StringUtils.isEmpty(towerInfo)) {
            return Result.fail();
        } else {
            return Result.ok(towerInfo);
        }
    }

    @ApiOperation(value = "更新铁塔信息")
    @PostMapping("updateTowerInfo")
    public Result updateTowerInfo(@RequestBody TowerInfo towerInfo) {
        boolean flag = towerInfoService.updateById(towerInfo);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "启用或停止启用铁塔")
    @PutMapping("lockTowerInfo/{id}/{status}")
    public Result lockTowerInfo(@PathVariable Long id,
                                @PathVariable Integer status){
        TowerInfo towerInfoServiceById = towerInfoService.getById(id);
        towerInfoServiceById.setStatus(status);
        towerInfoService.updateById(towerInfoServiceById);
        return Result.ok();
    }

    @ApiOperation(value = "批量删除铁塔信息")
    @DeleteMapping("batchRemove")
    public Result batchRemoveTowerInfo(@RequestBody List<Long> idList) {
        boolean flag = towerInfoService.removeByIds(idList);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }
}
